package com.banciyuan.bcywebview.utils.http;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ImageView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.utils.universalimageloader.core.DisplayImageOptions;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.core.assist.FailReason;
import com.banciyuan.bcywebview.utils.universalimageloader.core.assist.ImageScaleType;
import com.banciyuan.bcywebview.utils.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

/**
 * Created by squall on 2015/10/13.
 */
public class GifLoader {

    private ImageLoader imageLoader;

    public GifLoader() {
        this.imageLoader = ImageLoader.getInstance();
    }

    public void displayGif(final String url, final GifImageView imageView) {
        imageView.setImageResource(R.drawable.loading);
        new AsyncTask<Void, GifDrawable, GifDrawable>() {

            @Override
            protected GifDrawable doInBackground(Void... params) {
                try {
                    if (imageLoader.getDiskCache().get(url) != null) {
                        File mFile = imageLoader.getDiskCache().get(url);
                        return parseGif(mFile);
                    } else {
                        return null;
                    }
                } catch (Exception e) {
                    return null;
                }
            }

            @Override
            protected void onPostExecute(final GifDrawable gifDrawable) {
                if (gifDrawable == null) {
                    loadNetGif(url, imageView, imageLoader);
                } else {
                    imageView.setImageDrawable(gifDrawable);
                }
            }
        }.execute();
    }

    private GifDrawable parseGif(File mFile) throws Exception {
        InputStream is = new FileInputStream(mFile);
        BufferedInputStream bis = new BufferedInputStream(is);
        return new GifDrawable(bis);
    }


    private void loadNetGif(final String url, final ImageView imageView, final ImageLoader imageLoader) {

        DisplayImageOptions optionsDownload = new DisplayImageOptions.Builder()
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.NONE)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();

        imageLoader.loadImage(url, null, optionsDownload, new SimpleImageLoadingListener() {
            @Override
            public void onDiskCacheComplete(String imageUri, File mfile, String type) {
                try {
                    final GifDrawable gifDrawable = parseGif(mfile);
                    imageView.post(new Runnable() {
                        @Override
                        public void run() {
                            imageView.setImageDrawable(gifDrawable);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                imageView.setImageResource(R.drawable.loading_error);
            }
        }, null, false);
    }


}
